package com.wise.site.adapter;

import java.util.List;

import com.wise.site.R;
import com.wise.site.adapter.InfoListAdatper.ITEM_TYPE;
import com.wise.site.protocol.action.BusinessAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapItem;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.BussinesItemsResult;
import com.wise.site.protocol.result.BussnissItem;
import com.wise.site.protocol.result.InfoItemsResult;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.FileCache;
import com.wise.site.utils.ImageLoader;
import com.wise.site.utils.ImageLoader.Imager;
import com.wise.site.widget.PullRefenceList;
import com.wise.site.widget.PullRefenceList.OnRefreshListener;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BusinessAdapter extends ArrayListAdapter<SoapItem> {
	public enum BUSINESS_TYPE {
		// 出售
		SALE_TYPE,
		// 求购
		BUY_TYPE,
	}
	
	private boolean isAdd;

	private BUSINESS_TYPE type;
	private int searchType;
	private BusinessAction mAction;
	private ActionListener<BussinesItemsResult> mActionListener = new ActionListener<BussinesItemsResult>() {

		@Override
		public void onSucceed(BussinesItemsResult result) {
			if(result == null){
				return;
			}
			
			if(isAdd){
				addItems(result.list);
			} else {
				setList(result.list);
				FileCache.getInstance().put(type + "_" + mAction.getJsonParam("stype"), result, true);
			}
			refenceComplete(result.list);
		}

		@Override
		public void onError(int resultCode) {
			onSucceed((BussinesItemsResult)FileCache.getInstance().get(type + "_" + mAction.getJsonParam("stype")));
			refenceComplete(null);
		}

	};

	public BusinessAdapter(BUSINESS_TYPE type, Context context) {
		super(context);
		this.type = type;
		mAction = new BusinessAction(ACTION_TYPE.ACTION_COMMON, "gqlist");
		mAction.setActionListener(mActionListener);
	}
	
	public BusinessAdapter(BUSINESS_TYPE type, Context context, String method) {
		super(context);
		this.type = type;
		mAction = new BusinessAction(ACTION_TYPE.ACTION_COMMON, method);
		mAction.setActionListener(mActionListener);
	}
	
	public void addJsonParam(String param, Object value){
		mAction.addJsonParam(param, value);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (BUSINESS_TYPE.SALE_TYPE == type) {
			convertView = saleView(position, convertView);
		} else if (BUSINESS_TYPE.BUY_TYPE == type) {
			convertView = buyView(position, convertView);
		}

		return convertView;
	}
	
	public void setKeyword(String keyword){
		mAction.setKeyword(keyword);
	}

	public void setStype(int stype) {
		mAction.setStype(stype);
	}
	
	public void setShopId(int shopid){
		addJsonParam("shopid", shopid);
	}

	public BUSINESS_TYPE getType() {
		return type;
	}

	public void setType(BUSINESS_TYPE type) {
		this.type = type;
		mList.clear();
		removeAll();
	}

	public void setMemberID(int memberID) {
		mAction.setMemberID(memberID);
	}

	@Override
	public void setListView(ListView listView) {
		super.setListView(listView);
		listView.setAdapter(this);
		if (mListView instanceof PullRefenceList) {
			((PullRefenceList) mListView)
					.setonRefreshListener(new OnRefreshListener() {
						@Override
						public void onRefresh() {
							removeAll();
							moveToFristPage();
							loadData();
						}
					});
			
			((PullRefenceList) mListView).setFootOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					moveToNextPage();
					loadData(true);
				}
			});
		}
	}

	private View buyView(int position, View convertView) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.list_buy_item, null);
		}

		BussnissItem item = (BussnissItem) mList.get(position);
		convertView.setTag(item);

		((TextView) convertView.findViewById(R.id.title)).setText(item.title);
		((TextView) convertView.findViewById(R.id.snipping))
				.setText(item.description);
		return convertView;
	}

	private View saleView(int position, View convertView) {
		if(mList.size() <=  position){
			return null;
		}
		BussnissItem item = (BussnissItem) mList.get(position);
		convertView = inflater.inflate(R.layout.list_sale_item, null);
		
		convertView.setTag(item);
		ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
		if (item.iconURL != null && item.iconURL.length > 0 && item.iconURL[0].length() > 5) {
			if(icon != null){
				ImageLoader.getInstance().DisplayImage(new Imager(Constanct.IMAGE_PREFIX + item.iconURL[0], icon, mContext.getResources().getDimensionPixelSize(R.dimen.mult_ima_height), 0));
			}
		} else {
			icon.setImageResource(R.drawable.info_default);
			icon.invalidate();
		}

		((TextView) convertView.findViewById(R.id.title)).setText(item.title);
		((TextView) convertView.findViewById(R.id.snipping))
				.setText(item.description);
		((TextView) convertView.findViewById(R.id.price)).setText(item.price);
		((TextView) convertView.findViewById(R.id.commt_num))
				.setText(item.favoraterNum + "");
		return convertView;
	}

	public void moveToNextPage() {
		mAction.moveToNextPage();
	}

	public void moveToFristPage() {
		mAction.moveToFristPage();
	}
	
//	public void loadData(boolean isAdd, int type){
//		this.isAdd = isAdd;
//		showProgress();
//
//		mAction.setType(type);
//		ProtocolManager.getProtocolManager().submitAction(mAction);
//	}
	
	public void loadData(boolean isAdd) {
		this.isAdd = isAdd;
		showProgress();

		mAction.setType(searchType == 0 ? (type == BUSINESS_TYPE.SALE_TYPE ? 0 : 1) : searchType);
		ProtocolManager.getProtocolManager().submitAction(mAction);
	}

	public void setSearchType(int searchType) {
		this.searchType = searchType;
	}

	public void loadData() {
		loadData(false);
	}

}
